// pages/modes/mode.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    index: 0,
    currentOption: [],
    currentAnswers: "",
    selectAnswers: [],
    answersCard: {},
    show: false
  },

  onLookOptions() {
    this.setData({
      show: true
    })
  },

  onClose() {
    this.setData({
      show: false
    })
  },

  onChange(event) {
    this.setData({
      selectAnswers: event.detail
    })
  },

  onAnswersTop() {
    this.currentList(false)
    this.setData({
      index: this.data.index - 1,
    })
  },

  onAnswersBottom() {
    this.currentList(true)
    this.setData({
      index: this.data.index + 1,
    })
  },

  setCurrentAnswers(list) {
    this.setData({
      currentOption: JSON.parse(list[this.data.index].options),
      currentAnswers: JSON.parse(list[this.data.index].answers).join("")
    })
  },

  currentList(bln) {
    let index = this.data.index
    let answersCard = this.data.answersCard
    answersCard[index] = this.data.selectAnswers.sort().join("")
    let idx = bln ? index + 1 : index - 1
    this.setData({
      answersCard,
      selectAnswers: this.data.answersCard[idx] ? this.data.answersCard[idx].split("") : []
    })
  },

  getList() {
    wx.request({
      url: 'http://192.168.43.198:2345/exam/rand-list',
      // url: 'http://localhost:2345/exam/rand-list',
      success: res => {
        this.setData({
          list: res.data.data

        })
        this.setCurrentAnswers(this.data.list)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})